package com.hiv.service;

import cn.hutool.core.convert.Convert;
import com.google.common.collect.Lists;
import com.hiv.common.utils.AssertUtil;
import com.hiv.mapper.HospitalMapper;
import com.hiv.mapper.InspectionOfficeConfigMapper;
import com.hiv.mapper.domain.Admin;
import com.hiv.mapper.domain.Hospital;
import com.hiv.mapper.domain.InspectionOfficeConfig;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 检验所配置Service业务层处理
 *
 * @author zhanghong
 * @date 2023-04-10
 */
@Service
public class InspectionOfficeConfigService {
    @Autowired
    private InspectionOfficeConfigMapper inspectionOfficeConfigMapper;
    @Autowired
    private HospitalMapper hospitalMapper;

    /**
     * 查询检验所配置
     *
     * @return 检验所配置
     */
    public InspectionOfficeConfig getById(Admin admin) {
        AssertUtil.assertNotNull(admin.getHospitalId(), "检验所id不存在");

        InspectionOfficeConfig inspectionOfficeConfig = inspectionOfficeConfigMapper.selectByHospitalId(admin.getHospitalId());
        if (inspectionOfficeConfig == null && admin.getType() == 3) {
            inspectionOfficeConfig = new InspectionOfficeConfig();
            Hospital hospital = hospitalMapper.selectById(admin.getHospitalId());
            inspectionOfficeConfig.setBaseInspectionUnit(hospital.getName());
        } else {
            if (inspectionOfficeConfig != null && StringUtils.isEmpty(inspectionOfficeConfig.getBaseInspectionUnit()) && admin.getHospitalId() != null) {
                Hospital hospital = hospitalMapper.selectById(admin.getHospitalId());
                if (hospital != null) {
                    inspectionOfficeConfig.setBaseInspectionUnit(hospital.getName());
                }
            }
        }

        return inspectionOfficeConfig;
    }

    /**
     * 查询检验所配置列表
     *
     * @param inspectionOfficeConfig 检验所配置
     * @return 检验所配置
     */
    public List<InspectionOfficeConfig> selectList(InspectionOfficeConfig inspectionOfficeConfig) {
        return inspectionOfficeConfigMapper.selectList(inspectionOfficeConfig);
    }

    /**
     * 保存检验所配置
     *
     * @param inspectionOfficeConfig 检验所配置
     * @return 结果
     */
    public Boolean save(Admin admin, InspectionOfficeConfig inspectionOfficeConfig) {
        if (inspectionOfficeConfig.getId() != null) {
            inspectionOfficeConfigMapper.update(inspectionOfficeConfig);
            return true;
        } else {
            inspectionOfficeConfig.setHospitalId(admin.getHospitalId());
            inspectionOfficeConfigMapper.insert(inspectionOfficeConfig);
        }
        return true;
    }

    /**
     * 批量删除检验所配置
     *
     * @param ids 需要删除的检验所配置主键
     * @return 结果
     */
    public Boolean deleteByIds(String ids) {
        inspectionOfficeConfigMapper.deleteByIds(Convert.toStrArray(ids));
        return true;
    }

}
